package com.lqq.sparrow.container.proxy;

import com.lqq.sparrow.container.config.SpringRemoteServiceConfig;
import com.lqq.sparrow.proxy.ProxyFactory;
import com.lqq.sparrow.proxy.ProxyInstance;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 生成远程服务代理实例 <br/>
 * 容器实现这个类,实例化远程调用 <br/>
 * @author liangqq
 */
@Component
public class SpringProxyFactory extends ProxyFactory {

    @Autowired
    private SpringRemoteServiceConfig springRemoteServiceConfig;


    @Override
    public List<Class> getAllRemoteClass() {
        String classes = springRemoteServiceConfig.getClasses();
        return getAllRemoteClass(classes);
    }

    //临时方案
    public static List<ProxyInstance> getAllRemoteService(String classes) {
        List<Class> classList = getAllRemoteClass(classes);
        return new SpringProxyFactory().getAllRemoteService(classList);
    }

    public static List<Class> getAllRemoteClass(String classes) {
        //将来通过扫描自动获取所有需要动态代理的类.
        //String classes = springRemoteServiceConfig.getClasses();
        if(classes == null){
            return Collections.emptyList();
        }
        String[] classArr = classes.split(",");
        List<Class> classList = new ArrayList<>(classArr.length);
        for(String clazz: classArr){
            Class interfaceClass = null;
            try {
                interfaceClass = Class.forName(clazz);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            classList.add(interfaceClass);
        }
        return classList;
    }
}
